<?php
class Doajaxfileupload extends CI_Controller {
	
	public static function get_instance() {
		
	}
	
	function avatar(){		
		$this->load->helper('image');
		$error = "";
		$msg = "";
		
		$path = $_POST["path"];
		$name = $_POST["name"];
		
		$fileElementName = $name;  //传入文件元素
		$upload_dir=$path; //指定文件存储路径
		
		$upload_file=$_FILES[$fileElementName]["name"];        //获取文件名
		$upload_tmp_file=$_FILES[$fileElementName]["tmp_name"];      //获取临时文件名
		$upload_filetype=$_FILES[$fileElementName]["type"];    //获取文件类型
		$upload_filesize=$_FILES[$fileElementName]["size"];
		$upload_status=$_FILES[$fileElementName]["error"];  //获取文件出错情况
		$uploadFlag=true;
		
		$image_width = "300";
		$max_file = "3"; //最大3M
		$allowed_image_types = array('image/pjpeg'=>"jpg",'image/jpeg'=>"jpg",'image/jpg'=>"jpg",'image/png'=>"png",'image/x-png'=>"png",'image/gif'=>"gif");
		$allowed_image_ext = array_unique($allowed_image_types); // do not change this
		$image_ext="";
		foreach ($allowed_image_ext as $mime_type => $ext) {
			$image_ext.= strtoupper($ext)." ";
		}
		
		$filename = basename($_FILES[$fileElementName]['name']);
		$file_ext = strtolower(substr($filename, strrpos($filename, '.') + 1));

			
		//Only process if the file is a JPG, PNG or GIF and below the allowed limit
		if((!empty($_FILES[$fileElementName])) && ($upload_status == 0)) {
			foreach ($allowed_image_types as $mime_type => $ext) {
				if($file_ext==$ext && $upload_filetype==$mime_type){
					$error = "";
					break;
				}else{
					$error = "只支持【".$image_ext."】格式的图片，So...";
				}
			}

			if ($error=="" && $upload_filesize > ($max_file*1048576)) {
				$error.= "文件太大了，能不能小于 ".$max_file."M";
			}
		}
		else{
			$error= "请选择文件，谢谢~";
		}
		
		$uid =$this->session->userdata('user_id');
		function createAvatarFile($userid){
			$lev1=floor($userid/1000000);
			$lev1 =str_pad($lev1,3,"0",STR_PAD_LEFT);
			$lev2=floor($userid/10000);
			$lev2 =str_pad($lev2,2,"0",STR_PAD_LEFT);
			$lev3=floor($userid/100);
			$lev3=str_pad($lev3,2,"0",STR_PAD_LEFT);
			
			$fileDir="/".$lev1."/".$lev2."/".$lev3;
			return($fileDir);
		}
		
		$upload_dir .= createAvatarFile($uid);

		if($error =="" && is_uploaded_file($upload_tmp_file)){  
			
			if(file_exists($upload_dir) && is_dir($upload_dir)){
			}else{
				mkdir($upload_dir,0777,true);
			}
			
			switch($upload_status)
			{
				case 0: $error= "";break;
				case 1:	$error= "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。";break;
				case 2:	$error= "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。";break;
				case 3:	$error= "文件只有部分被上传。";break;
				case 4:	$error= "没有文件被上传。";break;
				case 6:	$error= "没有找到临时文件目录。";break;
				case 7:	$error= "文件写入失败。";break;
			}//分析文件出错情况并给出提示
			
			//生成文件路径
			$upload_path=$upload_dir."/".str_pad($uid,2,"0",STR_PAD_LEFT)."_avatar_".date("YmdHis",time()+8*3600).".".$ext;
			
			
			$width = getWidth($upload_tmp_file);
			$height = getHeight($upload_tmp_file);
			
			if($width>$height){
				$scale = $image_width/$height;
				$x=($width-$height)/2;
				$y=0;
				$side = $height;
			}
			else{
				$scale = $image_width/$width;
				$x=0;
				$y=($height-$width)/2;
				$side = $width;
			}
			
			//调整大小 为300*300
			resizeThumbnailImage($upload_path,$upload_tmp_file,$side,$side,$x,$y,$scale);
					
			if(is_uploaded_file($upload_tmp_file)){
		
				if(file_exists($upload_path)){	
					//echo "同名文件已经存在，请修改你要上传的文件名！";
					$uploadFlag=false;
					//检查是否有相同文件存在
				}else if(move_uploaded_file($upload_tmp_file,$upload_path)){
					//把文件从文件临时目录移动到上传目录
					$uploadFlag=true;
				}else{
					//echo"文件上传失败。";
					$uploadFlag=false;
				}
			}
			
			$upload_path=strtr($upload_path,"\\","/");			
		}
		else{  
	//echo "没有文件上传！";
			$upload_path=null;    
		}
		
		if($upload_path!=null){
			$msg=base_url().$upload_path;
		}else{
		}
	
		echo "{";
		echo				"error: '" . $error . "',\n";
		echo				"msg: '" . $msg . "'\n";
		echo "}";
	}
	
}
?>


